//
//  CYAddCreditVC.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/8/3.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYAddCreditVC.h"
#import "CYUser.h"
#import "CYHttpTool.h"
#import "CYLoginHttpTool.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "UIAlertView+Block.h"
#import "MBProgressHUD+LE.h"


@interface CYAddCreditVC ()<UIAlertViewDelegate>
@property (nonatomic, strong) NSString *IDStr;
@property (nonatomic, strong) NSString *statueStr;
@property (weak, nonatomic) IBOutlet UILabel *statueLab;
@property (nonatomic, strong) NSString *getStr;
@property (weak, nonatomic) IBOutlet UIButton *buyCardBtn;
@property (nonatomic, strong) NSString *buyStr;

@end

@implementation CYAddCreditVC

- (void)viewDidLoad {
    [super viewDidLoad];
     self.title = @"增值服务";
     
     [self loadstatues];
     
    // Do any additional setup after loading the view from its nib.
}

-(void)loadstatues{
     [MBProgressHUD showMessage:nil toView:self.view];

     CYUser *user = [CYLoginHttpTool userData].user;
     [CYHttpTool postWithURL:[CYURL shared].getCreditupGradeApply params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
          
           [MBProgressHUD hideHUDForView:self.view animated:YES];
          CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
          if (jsoninfo.isSuccess) {
               _statueStr = jsoninfo.message;
               _IDStr = jsoninfo.data;
                    _statueLab.text = jsoninfo.message;
               if (_IDStr && _IDStr.intValue == 1 ) {
                    _buyCardBtn.hidden = YES;
                    
               }
          }
          
     } failure:^(NSError *error) {
           [MBProgressHUD hideHUDForView:self.view animated:YES];
          CYLog(@"error = %@",error);
     }];
     
     [CYHttpTool postWithURL:[CYURL shared].getUpgradeMsg params:nil success:^(id json) {
          CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
          if (jsoninfo.isSuccess) {
               _buyStr = jsoninfo.message;
               
          }
          
     } failure:^(NSError *error) {
          
     }];
     
}

- (IBAction)addCreditClick:(id)sender {

     if (!_IDStr || !_statueStr || !_buyStr) {
          CYAlertView(@"网络出错，请检查您的网络");
          return;
     }
     
     if (_IDStr && _IDStr.intValue == 0 && _statueStr && _buyStr) {
          //可申请
          
          UIAlertView *buyalert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:_buyStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"购买", nil];
          [buyalert show];
          [buyalert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
               if (buttonIndex == 1) {
                    
                    CYUser *user = [CYLoginHttpTool userData].user;
                    [CYHttpTool postWithURL:[CYURL shared].addCreditupGradeApply params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
                         CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
                         CYAlertView(jsoninfo.message);
                         if (jsoninfo.isSuccess) {
                              _getStr = jsoninfo.data;
                              //通知，修改状态
                              [self loadstatues];
                         }
                         
                    } failure:^(NSError *error) {
                         
                         CYLog(@"error = %@",error);
                    }];

               }
               
              }];
          
          
              }else {
          CYAlertView(_statueStr);
          
     }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
